<template>
  <div class="list">
    <div class="content-head" style="margin-bottom: 20px;">
      <div class="sort-keys">
        <a-input placeholder="商品" style="width: 180px;margin-right:10px;" v-model="key" allowClear :size="size"
          @input="onSearch" />
        <span style="margin-left: 20px; margin-right: 6px;">日期:</span>
        <iDatePicker :showTime="false" style="margin-left: 10px;" placeholder="不选则不限制" v-model="filter.min_day"></iDatePicker>
        至
        <iDatePicker :showTime="false" style="margin-left: 0px;" placeholder="不选则不限制" v-model="filter.max_day"></iDatePicker>
        
        <a-select style="width: 150px;margin-left:10px;margin-right:10px;" v-model="admin_id" placeholder="操作人" allowClear v-if="action_show">
          <a-select-option :value="item.id" v-for="item in admin_list" :key="item.id">{{ item.title }}</a-select-option>
        </a-select>
        
        <a-select style="width: 150px;margin-left:10px;margin-right:10px;" v-model="type" placeholder="出入库类型" allowClear>
          <a-select-option :value="item.key" v-for="item in typeList" :key="item.key">{{ item.text }}</a-select-option>
        </a-select>
        
        <a-button :size="size" type="primary" @click="refreshData" style="margin-right:10px;">刷新</a-button>
      </div>

      <div style="margin-top: 4px;">
          <a-button type="primary" style="margin-right:10px;"  @click="isExport = !isExport">导出Excel</a-button>
      </div>
    </div>
    <a-table class="table" :rowKey="row => row.uuid" :dataSource="list" :pagination="pagination" :size="size">
      <a-table-column title="商品" width="120px">
        <template slot-scope="row">
          <div>
            {{row.xq && row.xq.title || '-'}}
          </div>
        </template>
      </a-table-column>
      <a-table-column title="变动数额" width="100px">
        <template slot-scope="row">
          <div v-if="row.type === 0" style="color: red;">
            - {{row.value}}
          </div>
          <div v-else style="color: #2BA246;">
             + {{row.value}}
          </div>
        </template>
      </a-table-column>
      <a-table-column title="操作人" width="100px">
        <template slot-scope="row">
          <div>
            {{row.admin && row.admin.name || '-'}}
          </div>
        </template>
      </a-table-column>
      <a-table-column title="变动描述" width="100px">
        <template slot-scope="row">
          <div>
            {{row.description }}
          </div>
           <div style="margin-top: 4px;">{{row.order_number}}</div>
        </template>
      </a-table-column>
      <a-table-column title="变动后库存" width="100px">
        <template slot-scope="row">
          <div>
            {{row.after}}
          </div>
        </template>
      </a-table-column>

    
      <a-table-column title="变动类型" width="80px">
        <template slot-scope="row">
          <span v-if="row.sub_type === 1">系统变动</span>
          <span v-else-if="row.sub_type === 2">后台单个修改</span>
        </template>
      </a-table-column>
      <a-table-column title="变动日期" dataIndex="created_at" width="80px">
        <template slot-scope="created_at">
          <span>{{ created_at | onlyDay }}</span>
        </template>
      </a-table-column>
    </a-table>

    <a-modal
      :title="(editUuid ? '编辑' : '创建') + resourceTypeText"
      style="top: 100px;"
      :visible="isEdit"
      width="1000px"
      @cancel="isEdit = false"
      :footer="null"
    >
      <EditForm v-if="isEdit" :uuid="editUuid" @cancel="isEdit = false" @success="fetchData"></EditForm>
    </a-modal>

    <ExportPopup @hide="isExport=false" :isShow="isExport"></ExportPopup>

  </div>
</template>
<script>
  import moment from 'moment'
  import ISwitch from '@/components/ISwitch/ISwitch'

  import ExportPopup from "./components/ExportRecord"
  import IDatePicker from "@/components/IDatePicker/IDatePicker.vue"

  const resourceType = 'storage-records'
  const resourceTypeText = '仓储库存操作记录'

  export default {
    components: {
      ISwitch,
      ExportPopup,
      IDatePicker
    },
    props: {
      selectMode: {
        type: Boolean
      },
      params: {
        type: Object
      },
      size: {
        type: String,
        default: 'default'
      }
    },
    data() {
      return {
        resourceTypeText,
        action_show: false,
        admin_list: [],
        admin_id: undefined,
        type: undefined,
        typeList: [     
          {
            text: '出库',
            key: '0'
          },
          {
            text: '入库',
            key: '1'
          }
        ],
        exportOptions: {
          status: 'all',
          startTime: '',
          endTime: '',
          type: 'normal'
        },
        list: [],
        page: 1,
        per_page: 10,
        total: 0,
        key: '',
        isExport: false,
        filter: {
          min_day: '',
          max_day: ''
        }
      }
    },
    watch: {
      params() {
        this.refreshData()
      },
      'filter.min_day'() {
        this.refreshData()
      },
      'filter.max_day'() {
        this.refreshData()
      }
    },
    computed: {
      pagination() {
        return {
          defaultPageSize: this.per_page,
          defaultCurrent: this.page,
          total: this.total,
          onChange: (e) => {
            this.page = e
            this.fetchData()
          }
        }
      }
    },
    filters: {},
    created() {
      this.action_show = this.$authCheck('storage_buylist')
      this.$http.get('/storage_admin_list').then(res => {
        this.admin_list = res.data.list
        this.admin_list = this.admin_list.map(item => {
          return {
            title: item.name,
            id: item.admin_id
          }
        })
      })
      this.fetchData()
    },
    methods: { 
      refreshData() {
        this.page = 1
        this.fetchData()
      },
      onSearch(e) {
		    this.page = 1
        this.fetchData()
      },
      fetchData() {
        this.isEdit = false
        this.$http.get(`/${resourceType}`, {
          params: {
            ...this.params,
            page: this.page,
            per_page: this.per_page,
            key: this.key,
            created_start_at: this.filter.min_day,
            created_end_at: this.filter.max_day,
            type: this.type,
            admin_id: this.admin_id
          }
        }).then(res => {
          this.list = res.data.list
          this.total = res.data.item_total
        }).finally(() => {
          this.loading = false
        })


      },
    }
  }
</script>
<style lang="scss" scoped>
  .value-font {
    font-weight: 500;
    font-size: 110%;
  }
  .content-head {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .table {
    .thumb {
      height: 60px;
      border-radius: 6px;
    }

    .content-image {
      height: 60px;
      width: 60px;
      margin-right: 10px;
      margin-bottom: 10px;
      object-fit: cover;
      border-radius: 10px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    .user {
      display: flex;
      // flex-wrap: wrap;
      align-items: center;
      img {
        width: 26px;
        height: 26px;
        margin-right: 6px;
        border-radius: 50%;
        overflow: hidden;
      }
      .gender-man, .gender-woman {
        margin-left: 5px;
        font-size: 12px;
      }
      .gender-man {
        color: #409EFF;
      }
      .gender-woman {
        color: #F56C6C;
      }
    }
  }
</style>
